package com.abc.recycling.mapper;

import com.abc.recycling.domain.MessageTemplate;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface MessageTemplateMapper {
    
    @Insert("INSERT INTO message_template(code, name, title, content, type, variables, status, remark, create_time) " +
            "VALUES(#{code}, #{name}, #{title}, #{content}, #{type}, #{variables}, #{status}, #{remark}, #{createTime})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insert(MessageTemplate template);
    
    @Update("UPDATE message_template SET code = #{code}, name = #{name}, title = #{title}, " +
            "content = #{content}, type = #{type}, variables = #{variables}, status = #{status}, " +
            "remark = #{remark}, update_time = #{updateTime} WHERE id = #{id}")
    int update(MessageTemplate template);
    
    @Select("SELECT * FROM message_template WHERE id = #{id}")
    MessageTemplate findById(Long id);
    
    @Select("SELECT * FROM message_template WHERE code = #{code}")
    MessageTemplate findByCode(String code);
    
    @Select("<script>" +
            "SELECT * FROM message_template " +
            "<where>" +
            "   <if test='type != null'> AND type = #{type} </if>" +
            "   <if test='status != null'> AND status = #{status} </if>" +
            "   <if test='keyword != null and keyword != \"\"'>" +
            "       AND (code LIKE CONCAT('%', #{keyword}, '%') " +
            "            OR name LIKE CONCAT('%', #{keyword}, '%'))" +
            "   </if>" +
            "</where>" +
            "ORDER BY create_time DESC" +
            "</script>")
    List<MessageTemplate> search(@Param("type") String type,
                               @Param("status") Integer status,
                               @Param("keyword") String keyword);
    
    @Delete("DELETE FROM message_template WHERE id = #{id}")
    int deleteById(Long id);
    
    @Update("UPDATE message_template SET status = #{status} WHERE id = #{id}")
    int updateStatus(@Param("id") Long id, @Param("status") Integer status);
} 